package com.qiantu.phone.ui.activity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.hjq.base.BaseDialog;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.entity.PanelBindConfig;
import com.qiantu.api.entity.PanelBindStatusBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.dialog.ConfirmCancelHintDialog;
import com.qiantu.phone.ui.dialog.ConfirmHintDialog;
import com.qiantu.phone.ui.dialog.WaitFinishDialog;
import com.qiantu.phone.ui.uiutil.PanelBindConfigUIUtil;

public class PanelBindInfoActivity extends AppActivity {
    private PanelBindConfigUIUtil panelBindConfigUIUtil;
    private SettingBar mDeviceName;
    private SettingBar mDeviceType;
    private SettingBar mDeviceInfo;
    private View unBindV;

    @Log
    public static void start(Context context, PanelBindConfig configData) {
        Intent intent = new Intent(context, PanelBindInfoActivity.class);
        intent.putExtra("configData", configData);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_panel_bind_info;
    }

    @Override
    protected void initView() {
        mDeviceName = findViewById(R.id.device_name);
        mDeviceType = findViewById(R.id.device_type);
        mDeviceInfo = findViewById(R.id.device_info);
        unBindV = findViewById(R.id.unbind);
        setOnClickListener(mDeviceInfo, mDeviceName, mDeviceType, unBindV);

        waitFinishDialog = new WaitFinishDialog.Builder(this)
                //.setIconWait(R.attr.icon_remind_load)
                .setTvContent(R.string.unbind_running)
                .create();

        unBindSuccessDialog = new ConfirmHintDialog.Builder(this)
                .setHint(getString(R.string.unbind_switch_success))
                //.setContent(String.format(getString(R.string.few_second_try), "3"))
                .setRightBtnText(getResources().getString(R.string.common_confirm))
                .setHintIcon(R.attr.icon_remind_correct)
                .create();
        unBindSuccessDialog.addOnDismissListener(new BaseDialog.OnDismissListener() {
            @Override
            public void onDismiss(BaseDialog dialog) {
                startActivity_SingleTask(PanelBindConfigActivity.class);
            }
        });

        unBindFailedDialog = new ConfirmHintDialog.Builder(this)
                .setHint(getString(R.string.unbind_switch_failed))
                .setContent(getString(R.string.please_after_try))
                .setHintIcon(R.attr.icon_remind_fail)
                .setRightBtnText(getResources().getString(R.string.common_confirm))
                .setRightBtnBackground(R.drawable.btn_secondary_color1_4dp)
                .create();
    }

    @Override
    protected void initData() {
        PanelBindConfig panelBindConfig = (PanelBindConfig) getIntent().getSerializableExtra("configData");
        panelBindConfigUIUtil = new PanelBindConfigUIUtil(getContext(), panelBindConfig);
        setTitle(String.format(getString(R.string.panel_config), panelBindConfigUIUtil.getBindUITypeStr()));
        mDeviceName.setLeftText(String.format(getString(R.string.item_name), panelBindConfigUIUtil.getBindUITypeStr()));
        mDeviceName.setRightText(panelBindConfigUIUtil.getConfigItemNameWithFloor());
        mDeviceType.setLeftText(String.format(getString(R.string.item_type), panelBindConfigUIUtil.getBindUITypeStr()));
        mDeviceType.setRightText(panelBindConfigUIUtil.getBindTypeStr());
        mDeviceInfo.setLeftText(String.format("实际对应%s", panelBindConfigUIUtil.isSceneItem()? getString(R.string.scene): getString(R.string.device)));
        mDeviceInfo.setRightText(panelBindConfigUIUtil.getConfigItemName());
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDeviceInfo){
            PanelBindSelectActivity.start(getAppActivity(), panelBindConfigUIUtil.config);
        }else if (view == unBindV){
            //先弹窗
            showConfirmCancelHintDialog(R.attr.icon_remind_hint, getString(R.string.hint_notice)
                    , getString(R.string.unbind_warning_tip), getString(R.string.close)
                    , getString(R.string.ok), new ConfirmCancelHintDialog.OnListener() {
                        @Override
                        public void onLeftClick(BaseDialog dialog) {
                            dialog.dismiss();
                        }

                        @Override
                        public void onRightClick(BaseDialog dialog) {
                            dialog.dismiss();
                            unbindSwitch();
                        }
                    });
        }
    }

    private void unbindSwitch() {
        showWaitFinishDialog();
        LLHttpManager.cancelPanelConfig(getAppActivity(), panelBindConfigUIUtil.config, new HttpCallback<HttpData<PanelBindConfig>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<PanelBindConfig> result) {
                super.onSucceed(result);
                checkPanelBindingStatus();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
                disWaitFinishDialog();
                unBindFailedDialog.show();
            }
        });
    }

    private int unbindCheckCount;
    private void checkPanelBindingStatus(){
        unbindCheckCount++;
        LLHttpManager.checkPanelBindingStatus(getAppActivity(), 0, panelBindConfigUIUtil.config.getSerialNo(), new HttpCallback<HttpData<PanelBindStatusBean>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<PanelBindStatusBean> result) {
                super.onSucceed(result);
                if (result.getData().status != 1){
                    if (unbindCheckCount<=3){
                        postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                checkPanelBindingStatus();
                            }
                        }, 2000);
                    }
                }else {
                    disWaitFinishDialog();
                    //弹出解绑成功
                    unBindSuccessDialog.show(3, new BaseDialog.DelayTimeCallBack() {
                        @Override
                        public void callback(int time) {
                            TextView textView = unBindSuccessDialog.getContentView().findViewById(R.id.tv_content);
                            textView.setText(String.format(getString(R.string.few_second_try), ""+time));
                            if (time == 0){
                                startActivity_SingleTask(PanelBindConfigActivity.class);
                            }
                        }
                    });
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
                disWaitFinishDialog();
                unBindFailedDialog.show();
            }
        });
    }

    private BaseDialog waitFinishDialog, unBindSuccessDialog, unBindFailedDialog;
    private ObjectAnimator waitFinishAnimator;//等待解绑完成转圈动画
    private void showWaitFinishDialog() {
        waitFinishDialog.show();

        if (waitFinishAnimator == null) {
            waitFinishAnimator = ObjectAnimator.ofFloat(waitFinishDialog.findViewById(R.id.icon_wait), "rotation", 0f, 360f)
                    .setDuration(1000);
            waitFinishAnimator.setRepeatCount(1000);
            waitFinishAnimator.setRepeatMode(ValueAnimator.RESTART);
            waitFinishAnimator.start();
            return;
        }
        waitFinishAnimator.cancel();
        waitFinishAnimator.start();

    }

    private void disWaitFinishDialog() {
        if (waitFinishDialog.isShowing()) {
            waitFinishAnimator.cancel();
            waitFinishDialog.dismiss();

        }
    }
}